//
//  HERoomUserTopView.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/2/26.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HERoomUserTopView.h"
#import "HEIMRoomUserCell.h"
#import "HECircleMoreViewController.h"
#import "HERoomUserDetailsViewController.h"

@interface HERoomUserTopView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;

@end
 
@implementation HERoomUserTopView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame: frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    self.backgroundColor = UIColor.whiteColor;
    
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    _layout.estimatedItemSize = CGSizeMake(120, 100);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    if (@available(iOS 11.0, *)) {
        _collectionView.contentInsetAdjustmentBehavior = NO;
    } else {
        // Fallback on earlier versions
    }
    [self addSubview:_collectionView];
     
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceHorizontal = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HEIMRoomUserCell.class forCellWithReuseIdentifier: @"HEIMRoomUserCell"];
    
    YYLabel *label = [[YYLabel alloc] init];
    label.font = DYFont(18);
    label.textColor = CNavBgColor;
    label.text = @"更多";
    label.verticalForm = YES; // 文字竖向展示
    label.textAlignment = NSTextAlignmentCenter;
    label.textVerticalAlignment = YYTextVerticalAlignmentTop; // 文字置顶显示
    label.exclusionPaths = @[[UIBezierPath bezierPathWithRect:CGRectZero]];
    [self addSubview:label];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(0);
        make.left.equalTo(self);
        make.right.equalTo(label.mas_left).offset(-15);
        make.height.offset(75);
        make.bottom.equalTo(self).mas_offset(-0);
    }];
 
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).mas_offset(0);
        make.right.equalTo(self).mas_offset(-15);
        make.height.offset(75);
    }];
    
    label.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toMoreVC)];
    [label addGestureRecognizer:tap];
}

- (void)toMoreVC {
    HECircleMoreViewController *vc = [HECircleMoreViewController new];
    vc.roomId = _superVC.roomId;
    [_superVC.navigationController pushViewController:vc animated:YES];
}

- (void)refreshData:(NSArray <HEIMKitInfo *>*)users {
    self.showList = [NSMutableArray arrayWithArray:users];
    [self.collectionView reloadData];
}
  
#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _showList.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEIMRoomUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"HEIMRoomUserCell" forIndexPath:indexPath];
    cell.model = _showList[indexPath.row];
     return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(60, 60);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(0, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
    HEIMKitInfo *info =  _showList[indexPath.row];
    HERoomUserDetailsViewController *vc = [HERoomUserDetailsViewController new];
    vc.user = info;
    vc.roomId = _superVC.roomId;
    [_superVC.navigationController pushViewController:vc animated:YES];
}
 


@end
